/*
 * Class
 * LanguageProvider
 */
package pl.abstractvoid.languages;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Provides internationalized messages for GUI.
 * 
 * @author Wojciech Szałapski
 * @author Ivo Zieliński
 */
public final class LanguageProvider {

    /**
     * The only instance of the provider.
     */
    private static LanguageProvider instance;
    
    /**
     * Locale defining what language is currently being used.
     */
    private Locale currentLocale;
    
    /**
     * Internationalized text for GUI.
     */
    private ResourceBundle messages;
    
    /**
     * Internationalized text for GUI concerning distributions' parameters.
     */
    private ResourceBundle distributionMessages;

    /**
     * Private constructor to prevent from creating multiple instances.
     */
    private LanguageProvider() {
        currentLocale = new Locale("pl");
        messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
        distributionMessages = ResourceBundle.getBundle("DistributionMessagesBundle", currentLocale);
    }

    /**
     * Returns the only instance of the language provider.
     * 
     * @return The only instance of the language provider.
     */
    public static LanguageProvider getInstance() {
        if (instance == null) {
            instance = new LanguageProvider();
        }
        return instance;
    }

    /**
     * Sets the language by it's code.
     * 
     * @param languageCode ISO-639 language code.
     */
    public void setLanguage(String languageCode) {
        switch (languageCode) {
            case "pl":
                currentLocale = new Locale("pl");
                break;
            case "en":
                currentLocale = new Locale("en");
                break;
        }
        messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
        distributionMessages = ResourceBundle.getBundle("DistributionMessagesBundle", currentLocale);
    }
    
    /**
     * Returns internationalized string for GUI according to language set.
     * 
     * @param name Name of the resource string to retrieve.
     * @return Internationalized string.
     */
    public String getString(String name) {
        return messages.getString(name);
    }
    
    /**
     * Returns internationalized distribution parameter string for GUI according
     * to language set.
     * 
     * @param name Name of the resource string to retrieve.
     * @return Internationalized string which is a distribution parameter.
     */
    public String getDistributionString(String name) {
        return distributionMessages.getString(name);
    }
    
    /**
     * Returns currently set language.
     * 
     * @return ISO-639 code of the current language.
     */
    public String getCurrentLanguageCode() {
        return currentLocale.getLanguage();
    }
    
    /**
     * Returns if the given language code is available in the program.
     * 
     * @param languageCode ISO-639 language code.
     * @return The flag representing if this language code pertains to an
     *         available language.
     */
    public boolean isAvailableLanguage(String languageCode) {
        return languageCode.equals("pl") || languageCode.equals("en");
    }
}
